package com.cl.short_video_java.web.video;

import com.cl.short_video_java.annotation.IsRole;
import com.cl.short_video_java.annotation.Log;
import com.cl.short_video_java.annotation.Login;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.menum.BusinessType;
import com.cl.short_video_java.service.SysSettingService;
import com.cl.short_video_java.service.VideoVideoService;
import com.cl.short_video_java.vo.video.InserytVideo;
import com.cl.short_video_java.vo.video.MangementSearch;
import com.cl.short_video_java.vo.video.VideoUpRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/video")
public class ManagementController {

	private VideoVideoService videoVideoService;
	private SysSettingService settingService;

	public ManagementController(VideoVideoService videoVideoService, SysSettingService settingService) {

		this.videoVideoService = videoVideoService;
		this.settingService = settingService;
	}

	/**
	 * 列表分页查询-已审核
	 *
	 * @param videoMangementSearch
	 * @return
	 */
	@PostMapping("/page")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	public Result getPageVideo(@RequestBody MangementSearch videoMangementSearch) {
		return Result.success(videoVideoService.getPageVideoService(videoMangementSearch));
	}


	/**
	 * 修改视频类型
	 *
	 * @return
	 */
	@PostMapping("/updata")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	@Log(title = "视频", businessType = BusinessType.UPDATE)
	public Result updataType(@Autowired @RequestBody VideoUpRequest videoUpRequest) {
		return Result.success(videoVideoService.updataTypeService(videoUpRequest));
	}


	/**
	 * 删除
	 *
	 * @param idlist
	 * @return
	 */
	@PostMapping("/del")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	@Log(title = "视频删除", businessType = BusinessType.DELETE)
	public Result delete(@RequestParam("idlist") String idlist) {
		return Result.success(videoVideoService.deleteService(idlist));
	}

	/**
	 * 视频审核
	 *
	 * @param idlist
	 * @param status
	 * @param httpServletRequest
	 * @return
	 */
	@PostMapping("/review")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 20)
	@Log(title = "视频审核", businessType = BusinessType.OTHER)
	public Result reviewVideo(@RequestParam("idlist") String idlist, @RequestParam("status") Integer status,
							  HttpServletRequest httpServletRequest) {
		return Result.success(videoVideoService.reviewVideoService(idlist, status, httpServletRequest));
	}


	/**
	 * 设置推荐
	 *
	 * @param isrecommend
	 * @param id
	 * @return
	 */
	@PostMapping("/set")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	@Log(title = "设置推荐", businessType = BusinessType.UPDATE)
	public Result setIsRecommend(@RequestParam("isrecommend") Integer isrecommend, @RequestParam("id") Integer id) {
		return Result.success(videoVideoService.setIsRecommendService(isrecommend, id));
	}

	/**
	 * 视频分片上传-项目路径形式
	 *
	 * @param chunk
	 * @param chunkIndex
	 * @param totalChunks
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/upvideo")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	@Log(title = "视频上传", businessType = BusinessType.INSERT)
	public Result upVideo(
			@RequestParam("chunk") MultipartFile chunk,
			@RequestParam("chunkIndex") Integer chunkIndex,
			@RequestParam("totalChunks") Integer totalChunks,
			@RequestParam("uuid") String uuid
	) throws Exception {
		if (settingService.getSysSettingByKey("is_oss").equals("1")){
			System.out.println("----------is_oss=1");
			return Result.success(videoVideoService.upVieoOssService(chunk,chunkIndex,totalChunks,uuid));
		}else {
			System.out.println("------------is_oss=0");
			return Result.success(videoVideoService.upVideoService(chunk,chunkIndex,totalChunks,uuid));
		}
	}

	/**
	 * 上传临时目录
	 * @param chunk
	 * @param chunkIndex
	 * @param totalChunks
	 * @param uuid
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/updatatest")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	public Result upVideoTem(@RequestParam("chunk") MultipartFile chunk,
							 @RequestParam("chunkIndex") Integer chunkIndex,
							 @RequestParam("totalChunks") Integer totalChunks,
							 @RequestParam("uuid") String uuid) throws IOException {
		return Result.success(videoVideoService.upVideoTemService(chunk,chunkIndex,totalChunks,uuid));
	}

	/**
	 * 视频分片合并
	 * @param uuid
	 * @param total
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/merge")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	public Result VideoMerge(@RequestParam("uuid") String uuid,@RequestParam("total") Integer total) throws IOException {
		return Result.success(videoVideoService.VideoMergeService(uuid,total));
	}
	@PostMapping("/delvideo")
	public String delVideo(@RequestParam("img") String img,@RequestParam("video") String video){
		return videoVideoService.delVideoService(img,video);
	}

	/**
	 * Oss视频上传
	 * @param chunk
	 * @param chunkIndex
	 * @param totalChunks
	 * @param uuid
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/video_oss")
    @Login(issuer = Const.GRADE_ADMIN)
    @IsRole(menuId = 19)
    @Log(title = "视频上传",businessType = BusinessType.INSERT)
    public Result upVieoOss(@RequestParam("chunk") MultipartFile chunk,
                            @RequestParam("chunkIndex") Integer chunkIndex,
                            @RequestParam("totalChunks") Integer totalChunks,
                            @RequestParam("uuid") String uuid) throws IOException {

			return Result.success(videoVideoService.upVieoOssService(chunk,chunkIndex,totalChunks,uuid));
    }

	/**
	 * 视频新增
	 * @param video
	 * @param httpServletRequest
	 * @return
	 * @throws Exception
	 */
    @PostMapping("/inser")
    @Log(title = "视频新增",businessType = BusinessType.INSERT)
    @Login(issuer = Const.GRADE_ADMIN)
    @IsRole(menuId = 19)
    public Result insertVideo(@Autowired @RequestBody InserytVideo video, HttpServletRequest httpServletRequest) throws Exception {
        Assert.isTrue(!video.getType().equals(""),"视频分类为空");
        return Result.success(videoVideoService.insertVideoService(video,httpServletRequest));
    }
	/**
	 * 视频上传--FTP
	 *
	 * @param chunk
	 * @param chunkIndex
	 * @param totalChunks
	 * @param uuid
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/ftp")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 19)
	@Log(title = "视频上传", businessType = BusinessType.INSERT)
	public ResponseEntity<?> upVideoFtp(
			@RequestParam("chunk") MultipartFile chunk,
			@RequestParam("chunkIndex") Integer chunkIndex,
			@RequestParam("totalChunks") Integer totalChunks,
			@RequestParam("uuid") String uuid
	) throws Exception {
		return videoVideoService.upVideoFtpService(chunk, chunkIndex, totalChunks, uuid);
	}

	/**
	 * 清除缓存
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping("/clearcache")
	public Result clearCache() throws JsonProcessingException {
		videoVideoService.getBank();
		return Result.success();
	}

}